﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using simpleHotel.Client.UserControls;
using simpleHotel.Business;
using simpleHotel.Client.HotelReference;

namespace simpleHotel.Client.Hotel
{
    public partial class ucUpdateEmptyRoom : BaseUserControl
    {
        private int _hotelId = -1;
        public int HotelId {
            get { return this._hotelId; }
            set { this._hotelId = value; }
        }
        
        public ucUpdateEmptyRoom()
        {
            InitializeComponent();
        }

        private void ucUpdateEmptyRoom_Load(object sender, EventArgs e)
        {
            this.SuspendLayout();
            this.tableLayout.SuspendLayout();
            
            InitTableLayout();

            //assign dates
            InitDates();

            this.tableLayout.ResumeLayout(false);
            this.tableLayout.PerformLayout();
            this.ResumeLayout();
            
        }

        private void InitDates()
        {
            var dict = new Dictionary<DayOfWeek, int> {
                { DayOfWeek.Monday,  0},
                { DayOfWeek.Tuesday, 1},
                { DayOfWeek.Wednesday,2},
                { DayOfWeek.Thursday, 3},
                { DayOfWeek.Friday, 4},
                { DayOfWeek.Saturday, 5},
                { DayOfWeek.Sunday, 6}            
            };

            int curDayOfWeek = dict[DateTime.Now.DayOfWeek];
            var now = DateTime.Now;
            for (int i = curDayOfWeek - 1, j = 1; i >= 0; i--)
            {
                var dt = now.Subtract(TimeSpan.FromDays(j++));
                var uc = (ucRoomItem)this.tableLayout.GetControlFromPosition(i, 1);
                uc.UpdateDate = dt;
            }
            for (int i = curDayOfWeek, j = 0; i < 7; i++)
            {
                var dt = now.Add(TimeSpan.FromDays(j++));
                var uc = (ucRoomItem)this.tableLayout.GetControlFromPosition(i, 1);
                uc.UpdateDate = dt;
            }
            for (int i = 2, k = 0; i < 5; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    var dt = now.Add(TimeSpan.FromDays(7 - curDayOfWeek + k++));
                    var uc = (ucRoomItem)this.tableLayout.GetControlFromPosition(j, i);
                    uc.UpdateDate = dt;
                }
            }
        }


        private void InitTableLayout()
        {
            //this.tableLayout.RowStyles.Clear();
            //this.tableLayout.ColumnStyles.Clear();
            //this.tableLayout.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            var hotelService = new HotelService();
            var dt = hotelService.GetRoomCategoriesByHotelId(this.HotelId);

            for (int j = 1; j < 5; j++)
                for (int i = 0; i < 7; i++)
                {
                    var roomItem = NewRoomItem(dt);
                    this.tableLayout.Controls.Add(roomItem, i, j);
                }
        }

        private ucRoomItem NewRoomItem(DataTable categories) {
            var item = new ucRoomItem 
            { 
                Categories = categories, 
                Dock = DockStyle.Fill,
                HotelId = this.HotelId
            };

            return item;
        }
    }
}
